package com.dycx.cms.converter;

import java.time.LocalDateTime;

import cn.hutool.core.bean.BeanUtil;
import com.dycx.common.vendor.oss.AliyunOss;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.cms.entity.AdDO;
import com.dycx.cms.vo.AdVO;
import com.dycx.cms.dto.AdDTO;

/**
 * 广告表(Ad)实体转换类
 *
 * @author wuzongbing
 * @since 2025-02-13 02:50:21
 */


@Data
public class AdConverter implements Serializable {
    public static AdDO dtoToEntity(AdDTO dto) {
        if (dto == null) return null;
        AdDO entity = BeanUtil.toBean(dto, AdDO.class);
        entity.setImage(AliyunOss.delDomain(entity.getImage()));
        return entity;
    }

    public static AdVO entityToVo(AdDO entity) {
        if (entity == null) return null;
        AdVO vo = BeanUtil.toBean(entity, AdVO.class);
        vo.setImage(AliyunOss.addDomain(vo.getImage()));
        return vo;
    }

    public static List<AdVO> entityToVoList(List<AdDO> entityList) {
        List<AdVO> voList = new ArrayList<>();
        for (AdDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<AdVO> entityToVoPage(Page<AdDO> page) {

        List<AdVO> newRecords = new ArrayList<>();

        List<AdDO> records = page.getRecords();
        for (AdDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<AdVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
